package com.zzuli.yxypre.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zzuli.yxypre.entity.PlayCount;
import com.zzuli.yxypre.mapper.PlayCountMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 定时任务：更新播放量
 * @auther pony
 * @create 2023-05-12 14:08
 */
@Component
public class updateCount {

    @Autowired
    private PlayCountMapper playCountMapper;

    /**
     * 更新每天的歌曲播放量
     */
    @Scheduled(cron = "0 0 0 * * ?") // 每天半夜 24 点更新更新数据
    public void updatePlayCountByDay(){
        // 1、先将所有的数据查出来
        LambdaQueryWrapper<PlayCount> pcQw = new LambdaQueryWrapper<>();
        List<PlayCount> playCountList = playCountMapper.selectList(pcQw);
        // 2、将昨天播放量修改为今天的播放量
        for (PlayCount playCount : playCountList) {
            playCount.setYesterdayCount(playCount.getDayCount());
            // 3、将今天的播放量清空
            playCount.setDayCount(0);
            playCountMapper.updateById(playCount);
        }
    }

    /**
     * 更新每周的歌曲播放量
     */
    @Scheduled(cron = "0 0 0 ? * 1") // 每周半夜 24 点更新更新数据
    public void updatePlayCountByWeek(){
        // 1、先将所有的数据查出来
        LambdaQueryWrapper<PlayCount> pcQw = new LambdaQueryWrapper<>();
        List<PlayCount> playCountList = playCountMapper.selectList(pcQw);
        for (PlayCount playCount : playCountList) {
            // 2、将每周的播放量清空
            playCount.setWeekCount(0);
            playCountMapper.updateById(playCount);
        }
    }

    /**
     * 更新每月的歌曲播放量
     */
    @Scheduled(cron = "0 0 0 1 * ?") // 每月1号 半夜 24 点更新更新数据
    public void updatePlayCountByMonth(){
        // 1、先将所有的数据查出来
        LambdaQueryWrapper<PlayCount> pcQw = new LambdaQueryWrapper<>();
        List<PlayCount> playCountList = playCountMapper.selectList(pcQw);
        for (PlayCount playCount : playCountList) {
            // 2、将每周的播放量清空
            playCount.setMonthCount(0);
            playCountMapper.updateById(playCount);
        }
    }

}
